# -*- coding: utf-8 -*-
"""
机器写作发送到CMS模块的代码
"""
from __future__ import print_function, unicode_literals
import requests
import json
from logger_utility.loggconfig import getLogger


logger = getLogger("BotSendCms")
URL_ONLINE = "http://10.26.90.52:10093/bot_writer_export/"
URL_TEST = "http://10.26.90.52:10094/bot_writer_export_test/"

cityIdNames = [
    "乐山市",
    "内江市",
    "凉山彝族自治州",
    "南充市",
    "宜宾市",
    "巴中市",
    "广元市",
    "广安市",
    "德阳市",
    "成都市",
    "攀枝花市",
    "泸州市",
    "甘孜藏族自治州",
    "眉山市",
    "绵阳市",
    "自贡市",
    "资阳市",
    "达州市",
    "遂宁市",
    "阿坝藏族羌族自治州",
    "雅安市",
    "北京市",
    "天津市",
    "上海市",
    "重庆市",
    "广西壮族自治区",
    "西藏自治区",
    "宁夏回族自治区",
]


def send_api(url, in_data):
    """
    发送给bot
    input_data =
    {
       'from_who':'xxx@thecover.co',
       'receiver':'CMS',
       'article_type':'test',
       'data':{
                'title':title,
                'content': content,
                'imgUrl': imgUrl,
                'kindName':'', # str 机器数据库中文章类型
                'cityIdNames':[ ], # list 文章城市名称数组，城市上限为10个
                'IsChina':0, # int  是否为全国新闻，1是全国新闻，其他不是
        }
    }
    :return:
    """
    try:
        ret = requests.post(url, data=json.dumps(in_data))
        ret_dict = ret.json()
        if ret_dict['msg_code'] != '00':
            logger.warning("Send To Bot Error! %s",
                           json.dumps(ret_dict, ensure_ascii=False))
            return False

        else:
            logger.info("Send To Bot Success! %s", in_data.get('data', {}).get('title'))
            logger.info("info: %s", json.dumps(ret_dict, ensure_ascii=False))
            return True

    except Exception as e:
        logger.exception("Send To Bot Except! %s", e)
        return None


def combine_discount(title, content):
    """
    合并打折资讯
    :param title:
    :param content:
    :return:
    """
    in_data = {
        'from_who': 'spirit@thecover.co',
        'receiver': 'CMS',
        'article_type': "打折资讯",
        'data': {
            'title': title,
            'content': content,
            'imgUrl': "http://7xte9b.com2.z0.glb.qiniucdn.com/o_1bv1scgtcbtino11dooocosoj9.png",
            'kindName': '打折资讯',
            'cityNames': ["成都市"],
            'isChina': 0,

        },
    }
    return in_data


def send_discount(title, content):
    """
    发送打折资讯
    :return:
    """
    return send_api(URL_ONLINE, combine_discount(title, content))


def send_discount_test(title, content):
    """
    发送测试环境
    :param title:
    :param content:
    :return:
    """
    return send_api(URL_TEST, combine_discount(title, content))


def combine_powercut(title, content):
    """
    发送停电资讯
    :return:
    """
    in_data = {
        'from_who': 'spirit@thecover.co',
        'receiver': 'CMS',
        'article_type': "水电气",
        'data': {
            'title': title,
            'content': content,
            'imgUrl': "http://7xte9b.com2.z0.glb.qiniucdn.com/o_1buq6pjddherolq2a215jd1ffrj.jpg",
            'kindName': '水电气',
            'cityNames': ["成都市"],
            'isChina': 0,
        },
    }
    return in_data


def send_powercut(title, content):
    """
    发送停电资讯
    :param title:
    :param content:
    :return:
    """
    return send_api(URL_ONLINE, combine_powercut(title, content))


def send_powercut_test(title, content):
    """
    发送测试停电资讯
    :param title:
    :param content:
    :return:
    """
    return send_api(URL_TEST, combine_powercut(title, content))


if __name__ == '__main__':
    pass
